<?php
/**
 * Created by PhpStorm.
 * User: kcj
 * Date: 2017/4/25
 * Time: 10:18
 */

namespace app\index\controller;

use app\index\model\UserModel;
use app\index\model\NoticeModel;
use think\Controller;

use GatewayClient\Gateway;

class Notice extends Controller
{
    protected function _initialize()
    {
        header("Access-Control-Allow-Origin: *");
    }

    public function addNotice()
    {
        print_r(db('notice')->select());
        print_r(iconv("UTF-8","GB2312",$_POST('title')));
        return true;
        $adminid=$_POST('adminid');
        if($adminid!='admin')return;
        $title=$_POST('title');
        $content=$_POST('content');
        $type=$_POST('type');
        $uids=$_POST('uid');


        
        $data['title'] =urldecode($title);
        $data['content']=urldecode($content);
        $data['publishtime']=date('Y-m-d H:i:s',time());
        $data['canceltime']=date('Y-m-d H:i:s',time());
        $data['deletetime']=date('Y-m-d H:i:s',time());
        $nm = new NoticeModel(null);
        $nm->add($data,$type,$uids);
    }

    public function removeallnotice()
    {
        $adminid=$_POST('adminid');
        if($adminid!='admin')return;
        $title=$_POST('title');
        $content=$_POST('content');
        $type=$_POST('type');
        $uids=$_POST('uid');

        $data['title'] =urldecode($title);
        $data['content']=urldecode($content);
        $data['publishtime']=date('Y-m-d H:i:s',time());
        $data['canceltime']=date('Y-m-d H:i:s',time());
        $data['deletetime']=date('Y-m-d H:i:s',time());
        $nm = new NoticeModel(null);
        $nm->add($data,$type,$uids);
    }

    public function getnoticelist($uid)
    {
        $user = new UserModel($uid);
        $notice = new NoticeModel($user);
        $list = $notice->getUserNoticelist();
        Gateway::$registerAddress=GATEWAYADDRESS;
        $json=array("type"=>'Notice.info',"data"=>$list);
        Gateway::sendToUid($uid,json_encode($json));
    }
    
    public function readnotice($uid,$noticeid)
    {
        $user = new UserModel($uid);
        $notice = new NoticeModel($user);
        $notice->read($noticeid);
        Gateway::$registerAddress=GATEWAYADDRESS;
        $json=array("type"=>'Notice.read',"data"=>$noticeid);
        Gateway::sendToUid($uid,json_encode($json));
    }
}